{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以MATH数据集为例,给出TOT的实现方案."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import argparse\n",
    "import copy\n",
    "import json\n",
    "import os\n",
    "import pickle\n",
    "import random\n",
    "import re\n",
    "import sys\n",
    "from collections import defaultdict, deque\n",
    "from typing import List\n",
    "import re\n",
    "from typing import Any, List\n",
    "import ipydagred3\n",
    "import networkx as nx\n",
    "import numpy as np\n",
    "import openai\n",
    "from tqdm import tqdm\n",
    "from utils import *\n",
    "\n",
    "#os.environ[\"http_proxy\"] = \"http://localhost:7890\"\n",
    "#os.environ[\"https_proxy\"] = \"http://localhost:7890\"\n",
    "setOpenAi(keyid = 4)\n",
    "\n",
    "GPT_MODEL = \"gpt-4-turbo\"\n",
    "GPT_MODEL = \"gpt-4o-mini\"\n",
    "#GPT_MODEL = \"gpt-3.5-turbo\"\n",
    "\n",
    "# 读取问题数据文件\n",
    "file_path = 'math200.json'\n",
    "N = 100\n",
    "with open(file_path, 'r', encoding='utf-8') as file:\n",
    "    problems = json.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def last_boxed_only_string(string):\n",
    "    idx = string.rfind(\"\\\\boxed\")\n",
    "    if idx < 0:\n",
    "        idx = string.rfind(\"\\\\fbox\")\n",
    "        if idx < 0:\n",
    "            return None\n",
    "\n",
    "    i = idx\n",
    "    right_brace_idx = None\n",
    "    num_left_braces_open = 0\n",
    "    while i < len(string):\n",
    "        if string[i] == \"{\":\n",
    "            num_left_braces_open += 1\n",
    "        if string[i] == \"}\":\n",
    "            num_left_braces_open -= 1\n",
    "            if num_left_braces_open == 0:\n",
    "                right_brace_idx = i\n",
    "                break\n",
    "        i += 1\n",
    "    \n",
    "    if right_brace_idx == None:\n",
    "        retval = None\n",
    "    else:\n",
    "        retval = string[idx:right_brace_idx + 1]\n",
    "    \n",
    "    return retval\n",
    "\n",
    "\n",
    "def remove_boxed(s):\n",
    "    left = \"\\\\boxed{\"\n",
    "    try:\n",
    "        assert s[:len(left)] == left\n",
    "        assert s[-1] == \"}\"\n",
    "        return s[len(left):-1]\n",
    "    except:\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = 'math200.json'\n",
    "N = 100\n",
    "count = 0\n",
    "questions = []\n",
    "types = []\n",
    "gold_answers = []\n",
    "# 打开文件并逐行读取内容\n",
    "with open(file_path, 'r', encoding='utf-8') as file:\n",
    "    dataset = json.load(file)\n",
    "\n",
    "for item in dataset:\n",
    "    questions.append(item['problem'])\n",
    "    types.append(item['type'])\n",
    "    gold_answers.append(item['solution'])\n",
    "    count += 1\n",
    "    if count >= N:\n",
    "        break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "question_id = 2\n",
    "\n",
    "question = problems[question_id]['problem']\n",
    "type = problems[question_id]['type']\n",
    "gold_answer = answer = remove_boxed(last_boxed_only_string(problems[question_id]['solution']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8\\\\sqrt{3} \\\\text { square inches}'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gold_answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The two triangles make a smaller hexagon inside the large hexagon with the same center. Draw six lines from the center to each of the vertices of the small hexagon. Both triangles are now divided into $9$ congruent equilateral triangles, with the smaller hexagon region taking $\\\\frac69=\\\\frac23$ of the triangle.\\n\\nThe triangle is $\\\\frac12$ of the larger hexagon, so the smaller hexagon is $\\\\frac12 \\\\cdot \\\\frac23 = \\\\frac13$ of the larger hexagon.\\n\\nWe now find the area of the large hexagon. By drawing six lines from the center to each of the vertices, we divide the hexagon into six equilateral triangles with side length $4$. The area of an equilateral triangle with side length $s$ is $\\\\frac{s^2 \\\\cdot \\\\sqrt{3}}{4}$, so the area of each triangle is $\\\\frac{16 \\\\sqrt{3}}{4}=4\\\\sqrt{3}$. Therefore, the area of the large hexagon is $24 \\\\sqrt{3}$. The area of the smaller hexagon, which is the region common to the two triangles, is $\\\\frac13 \\\\cdot 24 \\\\sqrt3=\\\\boxed{8\\\\sqrt{3} \\\\text { square inches}}$.'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "problems[question_id]['solution']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'By joining alternate vertices of a regular hexagon with edges $4$ inches long, two equilateral triangles are formed, as shown. What is the area, in square inches, of the region that is common to the two triangles? Express your answer in simplest radical form. [asy]\\ndraw((0,3)--(0,8)--(4,11)--(8,8)--(8,3)--(4,0)--cycle,black+linewidth(1));\\ndraw((4,0)--(0,8)--(8,8)--cycle, black+dashed+linewidth(1));\\ndraw((0,3)--(4,11)--(8,3)--cycle, black+dotted+linewidth(1));\\nlabel(\"4\",(8,5.5),E);\\n[/asy]'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Geometry'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "任务分解\n",
    "\n",
    "- scy的decompose_sql 只有一个output\n",
    "- 第二种我改的是有两个output， 改function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def decompose_sql(question, type):   \n",
    "\n",
    "    prompt_for_decompose = f\"\"\"I will now give you a math problem. The type of problem is {type}. Please help me translate this math problem into a series of step-by-step sub-problems.\n",
    "\n",
    "1 examples are as follows:\n",
    "Question: Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice 30 years old, how old is Kody? \n",
    "Answer: \n",
    "To solve the question \"How old is Kody?\", we need to solve the following problems step by step:\n",
    "1. How old is Mohamed now?\n",
    "2. How old was Mohamed four years ago?\n",
    "3. How old was Kody four years ago?\n",
    "4. How old is Kody now?\n",
    "\n",
    "Now the command is {question}, please decompose it into a series of easy-to-solve steps like the examples.\n",
    "Answer Format: (Please write each broken-down question step on a separate line, starting with a number.)\n",
    "To solve the question \"xxx\", we need to solve the following problems step by step:\n",
    "1. sub-question 1\n",
    "2. sub-question 2\n",
    "3. sub-question 3\n",
    "...\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "    Q = {\n",
    "        \"role\": \"user\",\n",
    "        \"content\": prompt_for_decompose\n",
    "    }\n",
    "    # Query = Example+[Q]\n",
    "    Query = [Q]\n",
    "    result = askChatGPT(Query, model=GPT_MODEL, temperature=1)\n",
    "    return result  , Query\n",
    "\n",
    "\n",
    "def convert_steps_to_format(decom_commands):\n",
    "    # 截取“we need to know:”后的内容\n",
    "    start_index = decom_commands.find(\"we need to solve the following problems step by step:\") + len(\"we need to solve the following problems step by step:\")\n",
    "    subtasks_text = decom_commands[start_index:].strip()\n",
    "    # 将每个子任务单独列出\n",
    "    subtasks = subtasks_text.split('\\n')\n",
    "    subtasks = [task.strip().split('. ', 1)[-1] for task in subtasks]\n",
    "    steps_dict = {index: value for index, value in enumerate(subtasks)}\n",
    "    return subtasks, steps_dict\n",
    "\n",
    "# decompose_steps = decompose_sql(question, type)\n",
    "# steps, steps_dict = convert_steps_to_format(decompose_steps)\n",
    "# num_steps = len(steps)\n",
    "# print(steps)\n",
    "# print(steps_dict)  # 只是加了一个问题的编号而已."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "print(len(steps))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total tokens: 330\n",
      "total tokens: 366\n",
      "total tokens: 322\n",
      "total tokens: 358\n",
      "total tokens: 323\n",
      "total tokens: 359\n",
      "total tokens: 541\n",
      "total tokens: 577\n",
      "total tokens: 529\n",
      "total tokens: 565\n",
      "total tokens: 529\n",
      "total tokens: 565\n",
      "total tokens: 522\n",
      "total tokens: 558\n",
      "total tokens: 517\n",
      "total tokens: 553\n",
      "total tokens: 526\n",
      "total tokens: 562\n",
      "total tokens: 776\n",
      "total tokens: 812\n",
      "total tokens: 785\n",
      "total tokens: 821\n",
      "total tokens: 781\n",
      "total tokens: 817\n",
      "total tokens: 734\n",
      "total tokens: 770\n",
      "total tokens: 715\n",
      "total tokens: 751\n",
      "total tokens: 719\n",
      "total tokens: 755\n",
      "total tokens: 1034\n",
      "total tokens: 1070\n",
      "total tokens: 1021\n",
      "total tokens: 1057\n",
      "total tokens: 1034\n",
      "total tokens: 1070\n",
      "total tokens: 1049\n",
      "total tokens: 1085\n",
      "total tokens: 1092\n",
      "total tokens: 1128\n",
      "total tokens: 1066\n",
      "total tokens: 1102\n",
      "total tokens: 1162\n",
      "total tokens: 1198\n",
      "total tokens: 1138\n",
      "total tokens: 1174\n",
      "total tokens: 1150\n",
      "total tokens: 1186\n",
      "total tokens: 1136\n",
      "total tokens: 1172\n",
      "total tokens: 1118\n",
      "total tokens: 1154\n",
      "total tokens: 1114\n",
      "total tokens: 1150\n",
      "total tokens: 1544\n",
      "total tokens: 1580\n",
      "total tokens: 1573\n",
      "total tokens: 1609\n",
      "total tokens: 1598\n",
      "total tokens: 1634\n",
      "total tokens: 1776\n",
      "total tokens: 1812\n",
      "total tokens: 1691\n",
      "total tokens: 1727\n",
      "total tokens: 1539\n",
      "total tokens: 1575\n",
      "total tokens: 1654\n",
      "total tokens: 1690\n",
      "total tokens: 1666\n",
      "total tokens: 1702\n",
      "total tokens: 1667\n",
      "total tokens: 1703\n",
      "total tokens: 1696\n",
      "total tokens: 1732\n",
      "total tokens: 1681\n",
      "total tokens: 1717\n",
      "total tokens: 1686\n",
      "total tokens: 1722\n",
      "2\n",
      "(33, ['The length of each side of the equilateral triangles formed by joining alternate vertices of the regular hexagon with edges of 4 inches is \\\\( 4\\\\sqrt{3} \\\\) inches.', 'To find the height of an equilateral triangle with side length \\\\( s \\\\), we can use the formula for the height \\\\( h \\\\):\\n\\n\\\\[\\nh = \\\\frac{\\\\sqrt{3}}{2}s\\n\\\\]\\n\\nGiven that the length of each side \\\\( s \\\\) of the triangle is \\\\( 4\\\\sqrt{3} \\\\) inches (as stated in the previous sub-problem), we can substitute this into the formula:\\n\\n\\\\[\\nh = \\\\frac{\\\\sqrt{3}}{2}(4\\\\sqrt{3}) = \\\\frac{4 \\\\cdot 3}{2} = 6 \\\\text{ inches}\\n\\\\]\\n\\nThus, the height of each equilateral triangle is \\\\( 6 \\\\) inches.', 'To find the area of one of the equilateral triangles, we can use the formula for the area \\\\( A \\\\) of an equilateral triangle with side length \\\\( s \\\\):\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4}s^2\\n\\\\]\\n\\nGiven that the length of each side \\\\( s \\\\) of the triangle is \\\\( 4\\\\sqrt{3} \\\\) inches, we can substitute this into the formula:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4}(4\\\\sqrt{3})^2\\n\\\\]\\n\\nCalculating \\\\( (4\\\\sqrt{3})^2 \\\\):\\n\\n\\\\[\\n(4\\\\sqrt{3})^2 = 16 \\\\cdot 3 = 48\\n\\\\]\\n\\nNow substitute back into the area formula:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4} \\\\cdot 48 = 12\\\\sqrt{3}\\n\\\\]\\n\\nTherefore, the area of one of the equilateral triangles is \\\\( 12\\\\sqrt{3} \\\\) square inches.', 'The two equilateral triangles formed by joining alternate vertices of the regular hexagon are positioned such that they overlap in a symmetrical manner. Each triangle shares two vertices with the hexagon, and since the triangles are constructed by joining every other vertex, they share a common central region that forms a smaller triangle at the center.\\n\\nTo visualize their positioning:\\n\\n1. One triangle is formed by connecting the vertices at positions 1, 3, and 5 of the hexagon, while the other is formed by connecting the vertices at positions 2, 4, and 6.\\n2. The intersection region is essentially a smaller equilateral triangle that is formed by the centroids of the two larger triangles since the shared area is symmetrical.\\n\\nTo find the area of this intersection, we can focus on the vertices of the hexagon and the symmetry of the triangles. The intersection of the two equilateral triangles results in a downscaled equilateral triangle, which can be determined to have a specific size proportional to the dimensions of the original triangles, but this requires further calculations that can be addressed next. \\n\\nIn summary, the two triangles intersect symmetrically around the center of the hexagon, forming a smaller equilateral triangle common to both.', 'The geometric shape formed by the intersection of the two equilateral triangles is another equilateral triangle. This smaller equilateral triangle is positioned symmetrically at the center of the larger triangles and is formed by the intersection of the segments connecting their vertices. Thus, the intersection region is a downscaled equilateral triangle common to both larger triangles.', 'To find the area of the intersection region formed by the two equilateral triangles, we can determine the side length of the smaller equilateral triangle that is formed at their intersection.\\n\\nThe larger equilateral triangles have a side length of \\\\(4\\\\sqrt{3}\\\\) inches. The smaller triangle that forms in the intersection of the two triangles, constructed from the centroids (which is also the center area), will have its side length as a fraction of the original triangles. \\n\\nGiven the symmetric nature of the construction and the properties of the hexagon, we can state that the side length of the smaller triangle is half that of the larger triangles:\\n\\n\\\\[\\n\\\\text{Side length of smaller triangle} = \\\\frac{1}{2} \\\\times (4\\\\sqrt{3}) = 2\\\\sqrt{3} \\\\text{ inches}\\n\\\\]\\n\\nNow, we can calculate the area of this smaller equilateral triangle using the area formula for an equilateral triangle:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4}s^2\\n\\\\]\\n\\nSubstituting the side length:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4}(2\\\\sqrt{3})^2\\n\\\\]\\n\\nCalculating \\\\( (2\\\\sqrt{3})^2 \\\\):\\n\\n\\\\[\\n(2\\\\sqrt{3})^2 = 4 \\\\cdot 3 = 12\\n\\\\]\\n\\nNow, substitute back into the area formula:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4} \\\\cdot 12 = 3\\\\sqrt{3}\\n\\\\]\\n\\nThus, the area of the intersection region formed by the two triangles is \\\\( \\\\boxed{3\\\\sqrt{3}} \\\\) square inches.', 'The area calculated in the previous step, \\\\( 3\\\\sqrt{3} \\\\) square inches, is already in its simplest radical form. There are no further simplifications that can be made. Thus, the answer is:\\n\\nThe area of the intersection region formed by the two triangles is \\\\( \\\\boxed{3\\\\sqrt{3}} \\\\) square inches.'])\n",
      "(33, ['The length of each side of the equilateral triangles formed by joining alternate vertices of the regular hexagon with edges of 4 inches is \\\\( 4\\\\sqrt{3} \\\\) inches.', 'To find the height of an equilateral triangle with side length \\\\( s \\\\), we can use the formula for the height \\\\( h \\\\):\\n\\n\\\\[\\nh = \\\\frac{\\\\sqrt{3}}{2}s\\n\\\\]\\n\\nGiven that the length of each side \\\\( s \\\\) of the triangle is \\\\( 4\\\\sqrt{3} \\\\) inches (as stated in the previous sub-problem), we can substitute this into the formula:\\n\\n\\\\[\\nh = \\\\frac{\\\\sqrt{3}}{2}(4\\\\sqrt{3}) = \\\\frac{4 \\\\cdot 3}{2} = 6 \\\\text{ inches}\\n\\\\]\\n\\nThus, the height of each equilateral triangle is \\\\( 6 \\\\) inches.', 'To find the area of one of the equilateral triangles, we can use the formula for the area \\\\( A \\\\) of an equilateral triangle with side length \\\\( s \\\\):\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4}s^2\\n\\\\]\\n\\nGiven that the length of each side \\\\( s \\\\) of the triangle is \\\\( 4\\\\sqrt{3} \\\\) inches, we can substitute this into the formula:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4}(4\\\\sqrt{3})^2\\n\\\\]\\n\\nCalculating \\\\( (4\\\\sqrt{3})^2 \\\\):\\n\\n\\\\[\\n(4\\\\sqrt{3})^2 = 16 \\\\cdot 3 = 48\\n\\\\]\\n\\nNow substitute back into the area formula:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4} \\\\cdot 48 = 12\\\\sqrt{3}\\n\\\\]\\n\\nTherefore, the area of one of the equilateral triangles is \\\\( 12\\\\sqrt{3} \\\\) square inches.', 'The two equilateral triangles formed by joining alternate vertices of the regular hexagon are positioned such that they overlap in a symmetrical manner. Each triangle shares two vertices with the hexagon, and since the triangles are constructed by joining every other vertex, they share a common central region that forms a smaller triangle at the center.\\n\\nTo visualize their positioning:\\n\\n1. One triangle is formed by connecting the vertices at positions 1, 3, and 5 of the hexagon, while the other is formed by connecting the vertices at positions 2, 4, and 6.\\n2. The intersection region is essentially a smaller equilateral triangle that is formed by the centroids of the two larger triangles since the shared area is symmetrical.\\n\\nTo find the area of this intersection, we can focus on the vertices of the hexagon and the symmetry of the triangles. The intersection of the two equilateral triangles results in a downscaled equilateral triangle, which can be determined to have a specific size proportional to the dimensions of the original triangles, but this requires further calculations that can be addressed next. \\n\\nIn summary, the two triangles intersect symmetrically around the center of the hexagon, forming a smaller equilateral triangle common to both.', 'The geometric shape formed by the intersection of the two equilateral triangles is another equilateral triangle. This smaller equilateral triangle is positioned symmetrically at the center of the larger triangles and is formed by the intersection of the segments connecting their vertices. Thus, the intersection region is a downscaled equilateral triangle common to both larger triangles.', 'To find the area of the intersection region formed by the two equilateral triangles, we can determine the side length of the smaller equilateral triangle that is formed at their intersection.\\n\\nThe larger equilateral triangles have a side length of \\\\(4\\\\sqrt{3}\\\\) inches. The smaller triangle that forms in the intersection of the two triangles, constructed from the centroids (which is also the center area), will have its side length as a fraction of the original triangles. \\n\\nGiven the symmetric nature of the construction and the properties of the hexagon, we can state that the side length of the smaller triangle is half that of the larger triangles:\\n\\n\\\\[\\n\\\\text{Side length of smaller triangle} = \\\\frac{1}{2} \\\\times (4\\\\sqrt{3}) = 2\\\\sqrt{3} \\\\text{ inches}\\n\\\\]\\n\\nNow, we can calculate the area of this smaller equilateral triangle using the area formula for an equilateral triangle:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4}s^2\\n\\\\]\\n\\nSubstituting the side length:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4}(2\\\\sqrt{3})^2\\n\\\\]\\n\\nCalculating \\\\( (2\\\\sqrt{3})^2 \\\\):\\n\\n\\\\[\\n(2\\\\sqrt{3})^2 = 4 \\\\cdot 3 = 12\\n\\\\]\\n\\nNow, substitute back into the area formula:\\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4} \\\\cdot 12 = 3\\\\sqrt{3}\\n\\\\]\\n\\nThus, the area of the intersection region formed by the two triangles is \\\\( \\\\boxed{3\\\\sqrt{3}} \\\\) square inches.', 'The area calculated in the previous step, \\\\(3\\\\sqrt{3}\\\\) square inches, is already in its simplest radical form. There are no further simplifications possible for this expression because \\\\(3\\\\) and \\\\(\\\\sqrt{3}\\\\) cannot be reduced or combined further.\\n\\nThus, the answer remains \\\\( \\\\boxed{3\\\\sqrt{3}} \\\\) square inches, which is already in its simplest form.'])\n"
     ]
    }
   ],
   "source": [
    "# TOT 求解\n",
    "N = 3  # 每个子问题进行N次proposal\n",
    "M = 2  # 通过评估选出M个最好的proposal\n",
    "\n",
    "solution = []\n",
    "\n",
    "for i in range(num_steps):\n",
    "    subtask = steps[i]\n",
    "    sys_q = f\"\"\"\n",
    "There is a math_problem. I need you to solve it and give an answer.\n",
    "Here is the problem:\\n{question}\n",
    "\n",
    "I have broken this math problem down into a series of smaller problems. I will assign you sub-problems one by one, and provide the results of the previous sub-problems as a reference for your reasoning.\n",
    "Please solve the problem and respond according to mathematical logic.\n",
    "\"\"\"  \n",
    "\n",
    "    subask = f\"\"\"\\nThe sub-problem to solve now is xxx: {subtask}\n",
    "Based on the information above, please provide a concise and clear answer\"\"\"\n",
    "    \n",
    "    if len(solution)==0:\n",
    "        # 第一个子问题\n",
    "        query = subask\n",
    "        Q = [{'role':'system', 'content':sys_q},\n",
    "            {'role':'user', 'content':query},]\n",
    "        for n in range(N):  # 一个子问题提问N次,获取N个解\n",
    "            result = askChatGPT(Q, model=GPT_MODEL, temperature=1)\n",
    "            eval_Q = Q + [{'role':'assistant', 'content':result}]\n",
    "            eval_Q = eval_Q + [{'role':'user', 'content':\"Please provide a confidence rating for the accuracy of this solution, on a scale from 1 to 5. Only output the number.\"}]\n",
    "            score = askChatGPT(eval_Q, model=GPT_MODEL, temperature=1)\n",
    "            score = int(score)\n",
    "            \n",
    "            solution.append((score, [result]))  # 维护一整条推理路径\n",
    "            \n",
    "        solution = sorted(solution, key=lambda x: x[0])\n",
    "        solution = solution[:M]  # 剪枝\n",
    "    else:\n",
    "        temp_solution = []\n",
    "        for m in range(M):  # 因为剪枝动态维护M个推理路径\n",
    "            answersSoFar = f\"\"\"\\nSo far, the answers to the preceding sub-problems are as follows: The format is Sub-problem-Id: xxx; Sub-problem: xxx; Answer: xxx.\"\"\"\n",
    "            for index, value in enumerate(solution[m][1]):\n",
    "                try:\n",
    "                    answersSoFar += f\"\"\"\\nSub-problem-Id: {index}; Sub-problem: {steps[index]}; Answer: {value}.\"\"\"\n",
    "                except:\n",
    "                    print('warning')\n",
    "                    print(index)\n",
    "                    print(len(solution[m][1]))\n",
    "                    print(len(steps))\n",
    "                    sys.exit(0)\n",
    "            query = answersSoFar+subask\n",
    "            Q = [{'role':'system', 'content':sys_q},\n",
    "                 {'role':'user', 'content':query},]\n",
    "            for n in range(N):  # 一个子问题提问N次,获取N个解\n",
    "                result = askChatGPT(Q, model=GPT_MODEL, temperature=1)\n",
    "                eval_Q = Q + [{'role':'assistant', 'content':result}]\n",
    "                eval_Q = eval_Q + [{'role':'user', 'content':\"Please provide a confidence rating for the accuracy of this solution, on a scale from 1 to 5. Only output the number.\"}]\n",
    "                score = askChatGPT(eval_Q, model=GPT_MODEL, temperature=1)\n",
    "                score = int(score)\n",
    "                \n",
    "                temp_solution.append((solution[m][0]+score, solution[m][1]+[result]))  # 路径score累加\n",
    "        \n",
    "        # print(len(temp_solution))  # 此时temp_solution中应该有M*N种推理路径\n",
    "        solution = sorted(temp_solution, key=lambda x: x[0])\n",
    "        solution = solution[:M]  # 剪枝 M*N->M\n",
    "\n",
    "print(len(solution))\n",
    "printSeq(solution)\n",
    "# 用额外的一次query再问一下最终的答案\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我刚刚看了一下tot，他每个问题的过程的方法好像都不太一样，他那个写作是对于任务分解做N次，然后选M个最好的，然后再根据选出来的plan推理N次，最后得出一个最好的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total tokens: 546\n",
      "{'role': 'user', 'content': 'I will now give you a math problem. The type of problem is Geometry. Please help me translate this math problem into a series of step-by-step sub-problems.\\n\\n1 examples are as follows:\\nQuestion: Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice 30 years old, how old is Kody? \\nAnswer: \\nTo solve the question \"How old is Kody?\", we need to solve the following problems step by step:\\n1. How old is Mohamed now?\\n2. How old was Mohamed four years ago?\\n3. How old was Kody four years ago?\\n4. How old is Kody now?\\n\\nNow the command is By joining alternate vertices of a regular hexagon with edges $4$ inches long, two equilateral triangles are formed, as shown. What is the area, in square inches, of the region that is common to the two triangles? Express your answer in simplest radical form. [asy]\\ndraw((0,3)--(0,8)--(4,11)--(8,8)--(8,3)--(4,0)--cycle,black+linewidth(1));\\ndraw((4,0)--(0,8)--(8,8)--cycle, black+dashed+linewidth(1));\\ndraw((0,3)--(4,11)--(8,3)--cycle, black+dotted+linewidth(1));\\nlabel(\"4\",(8,5.5),E);\\n[/asy], please decompose it into a series of easy-to-solve steps like the examples.\\nAnswer Format: (Please write each broken-down question step on a separate line, starting with a number.)\\nTo solve the question \"xxx\", we need to solve the following problems step by step:\\n1. sub-question 1\\n2. sub-question 2\\n3. sub-question 3\\n...\\n\\n'}\n",
      "{'role': 'assistant', 'content': \"The plan is : ['What is the length of each side of the equilateral triangles formed from the hexagon?', 'What is the height of each equilateral triangle?', 'What is the area of one equilateral triangle?', 'What is the angle of intersection between the two equilateral triangles?', 'What is the coordinates or vertices of the overlapping region?', 'What is the area of the overlapping region between the two triangles using the angle found in step 4 and the dimensions from step 3?', 'What is the final answer in simplest radical form for the area of the common region?']\"}\n",
      "{'role': 'user', 'content': 'Please provide a confidence rating for the accuracy of this step to step plan, on a scale from 1 to 10. Only output the number.'}\n",
      "total tokens: 547\n",
      "total tokens: 563\n",
      "{'role': 'user', 'content': 'I will now give you a math problem. The type of problem is Geometry. Please help me translate this math problem into a series of step-by-step sub-problems.\\n\\n1 examples are as follows:\\nQuestion: Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice 30 years old, how old is Kody? \\nAnswer: \\nTo solve the question \"How old is Kody?\", we need to solve the following problems step by step:\\n1. How old is Mohamed now?\\n2. How old was Mohamed four years ago?\\n3. How old was Kody four years ago?\\n4. How old is Kody now?\\n\\nNow the command is By joining alternate vertices of a regular hexagon with edges $4$ inches long, two equilateral triangles are formed, as shown. What is the area, in square inches, of the region that is common to the two triangles? Express your answer in simplest radical form. [asy]\\ndraw((0,3)--(0,8)--(4,11)--(8,8)--(8,3)--(4,0)--cycle,black+linewidth(1));\\ndraw((4,0)--(0,8)--(8,8)--cycle, black+dashed+linewidth(1));\\ndraw((0,3)--(4,11)--(8,3)--cycle, black+dotted+linewidth(1));\\nlabel(\"4\",(8,5.5),E);\\n[/asy], please decompose it into a series of easy-to-solve steps like the examples.\\nAnswer Format: (Please write each broken-down question step on a separate line, starting with a number.)\\nTo solve the question \"xxx\", we need to solve the following problems step by step:\\n1. sub-question 1\\n2. sub-question 2\\n3. sub-question 3\\n...\\n\\n'}\n",
      "{'role': 'assistant', 'content': \"The plan is : ['What are the coordinates of the vertices of the regular hexagon?', 'Which vertices are used to form the two equilateral triangles?', 'What are the lengths of the sides of the equilateral triangles formed?', 'What are the coordinates of the vertices of the first equilateral triangle?', 'What are the coordinates of the vertices of the second equilateral triangle?', 'How do we find the intersection points of the two equilateral triangles?', 'What is the area of the region formed by the intersection of the two triangles?', 'How do we express the final area in simplest radical form?']\"}\n",
      "{'role': 'user', 'content': 'Please provide a confidence rating for the accuracy of this step to step plan, on a scale from 1 to 10. Only output the number.'}\n",
      "total tokens: 550\n",
      "total tokens: 562\n",
      "{'role': 'user', 'content': 'I will now give you a math problem. The type of problem is Geometry. Please help me translate this math problem into a series of step-by-step sub-problems.\\n\\n1 examples are as follows:\\nQuestion: Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice 30 years old, how old is Kody? \\nAnswer: \\nTo solve the question \"How old is Kody?\", we need to solve the following problems step by step:\\n1. How old is Mohamed now?\\n2. How old was Mohamed four years ago?\\n3. How old was Kody four years ago?\\n4. How old is Kody now?\\n\\nNow the command is By joining alternate vertices of a regular hexagon with edges $4$ inches long, two equilateral triangles are formed, as shown. What is the area, in square inches, of the region that is common to the two triangles? Express your answer in simplest radical form. [asy]\\ndraw((0,3)--(0,8)--(4,11)--(8,8)--(8,3)--(4,0)--cycle,black+linewidth(1));\\ndraw((4,0)--(0,8)--(8,8)--cycle, black+dashed+linewidth(1));\\ndraw((0,3)--(4,11)--(8,3)--cycle, black+dotted+linewidth(1));\\nlabel(\"4\",(8,5.5),E);\\n[/asy], please decompose it into a series of easy-to-solve steps like the examples.\\nAnswer Format: (Please write each broken-down question step on a separate line, starting with a number.)\\nTo solve the question \"xxx\", we need to solve the following problems step by step:\\n1. sub-question 1\\n2. sub-question 2\\n3. sub-question 3\\n...\\n\\n'}\n",
      "{'role': 'assistant', 'content': \"The plan is : ['What is the length of each side of the equilateral triangles formed?', 'What is the height of each equilateral triangle?', 'What is the area of one of the equilateral triangles?', 'What are the coordinates of the vertices of the regular hexagon?', 'What are the coordinates of the vertices of each equilateral triangle?', 'How can we determine the points of intersection between the two triangles?', 'What is the area of the region that is common to both triangles using the coordinates of the intersection points?', 'Is the area expressed in simplest radical form? If not, how can it be simplified?']\"}\n",
      "{'role': 'user', 'content': 'Please provide a confidence rating for the accuracy of this step to step plan, on a scale from 1 to 10. Only output the number.'}\n",
      "total tokens: 555\n",
      "******************************解题阶段******************************\n",
      "total tokens: 1451\n",
      "{'role': 'user', 'content': '\\nThere is a math_problem. I need you to solve it and give an answer.\\nHere is the problem:\\nBy joining alternate vertices of a regular hexagon with edges $4$ inches long, two equilateral triangles are formed, as shown. What is the area, in square inches, of the region that is common to the two triangles? Express your answer in simplest radical form. [asy]\\ndraw((0,3)--(0,8)--(4,11)--(8,8)--(8,3)--(4,0)--cycle,black+linewidth(1));\\ndraw((4,0)--(0,8)--(8,8)--cycle, black+dashed+linewidth(1));\\ndraw((0,3)--(4,11)--(8,3)--cycle, black+dotted+linewidth(1));\\nlabel(\"4\",(8,5.5),E);\\n[/asy]\\n\\nI have broken this math problem down into a series of smaller problems. Here is the list of sub-problems: {0: \\'What are the coordinates of the vertices of the regular hexagon?\\', 1: \\'Which vertices are used to form the two equilateral triangles?\\', 2: \\'What are the lengths of the sides of the equilateral triangles formed?\\', 3: \\'What are the coordinates of the vertices of the first equilateral triangle?\\', 4: \\'What are the coordinates of the vertices of the second equilateral triangle?\\', 5: \\'How do we find the intersection points of the two equilateral triangles?\\', 6: \\'What is the area of the region formed by the intersection of the two triangles?\\', 7: \\'How do we express the final area in simplest radical form?\\'}\\nPlease solve the problem and respond according to mathematical logic.\\n    \\n    '}\n",
      "{'role': 'assistant', 'content': \"To solve the problem, we will work through the sub-problems you provided.\\n\\n### Step 0: What are the coordinates of the vertices of the regular hexagon?\\n\\nThe hexagon has vertices given by:\\n- \\\\( A(4, 0) \\\\)\\n- \\\\( B(8, 4) \\\\)\\n- \\\\( C(4, 8) \\\\)\\n- \\\\( D(0, 8) \\\\)\\n- \\\\( E(-4, 4) \\\\)\\n- \\\\( F(0, 0) \\\\)\\n\\n### Step 1: Which vertices are used to form the two equilateral triangles?\\n\\nBy joining alternate vertices, the triangles formed are:\\n1. Triangle formed by vertices \\\\( A(4, 0), C(4, 8), E(-4, 4) \\\\).\\n2. Triangle formed by vertices \\\\( B(8, 4), D(0, 8), F(0, 0) \\\\).\\n\\n### Step 2: What are the lengths of the sides of the equilateral triangles formed?\\n\\nEach side of the hexagon measures \\\\( 4 \\\\) inches. The triangles are equilateral since each segment joins two vertices separated by one vertex. The lengths of sides for both triangles can be calculated using the distance formula.\\n\\n**For Triangle \\\\( ACE \\\\):**\\n- \\\\( AC = \\\\sqrt{(4-4)^2 + (0-8)^2} = \\\\sqrt{64} = 8 \\\\) inches.\\n- \\\\( CE = \\\\sqrt{(4-(-4))^2 + (8-4)^2} = \\\\sqrt{(8^2 + 4^2)} = \\\\sqrt{80} = 4\\\\sqrt{5} \\\\) inches.\\n- \\\\( EA = \\\\sqrt{(4-(-4))^2 + (0-4)^2} = \\\\sqrt{(8^2 + 4^2)} = \\\\sqrt{80} = 4\\\\sqrt{5} \\\\) inches.\\n\\n**For Triangle \\\\( BDF \\\\):**\\n- \\\\( BD = \\\\sqrt{(8-0)^2 + (4-8)^2} = \\\\sqrt{(8^2 + (-4)^2)} = \\\\sqrt{80} = 4\\\\sqrt{5} \\\\) inches.\\n- \\\\( DF = \\\\sqrt{(0-0)^2 + (8-0)^2} = \\\\sqrt{(8^2)} = 8 \\\\) inches.\\n- \\\\( FB = \\\\sqrt{(0-8)^2 + (0-4)^2} = \\\\sqrt{(8^2 + (-4)^2)} = \\\\sqrt{80} = 4\\\\sqrt{5} \\\\) inches.\\n\\n### Step 3: What are the coordinates of the vertices of the first equilateral triangle?\\n\\nThe first equilateral triangle has vertices:\\n- \\\\( A(4, 0) \\\\)\\n- \\\\( C(4, 8) \\\\)\\n- \\\\( E(-4, 4) \\\\)\\n\\n### Step 4: What are the coordinates of the vertices of the second equilateral triangle?\\n\\nThe second equilateral triangle has vertices:\\n- \\\\( B(8, 4) \\\\)\\n- \\\\( D(0, 8) \\\\)\\n- \\\\( F(0, 0) \\\\)\\n\\n### Step 5: How do we find the intersection points of the two equilateral triangles?\\n\\nTo find the intersection, we can use line equations from triangle vertices:\\n\\n1. **From triangle ACE:**\\n   - **AE:** Slope = \\\\( \\\\frac{4-0}{-4-4} = -\\\\frac{1}{2} \\\\) (Line equation \\\\( y = -\\\\frac{1}{2}x + 2 \\\\))\\n   - **EC:** Vertical line or constant at \\\\( x = 4 \\\\).\\n\\n2. **From triangle BDF:**\\n   - **BD:** Slope = \\\\( \\\\frac{8-4}{0-8} = -\\\\frac{1}{2} \\\\) (Line equation \\\\( y = -\\\\frac{1}{2}x + 8 \\\\))\\n   - **DF:** Vertical line or constant at \\\\( x = 0 \\\\).\\n\\nFinding intersections would lead us to find common points.\\n\\n### Step 6: What is the area of the region formed by the intersection of the two triangles?\\n\\nThe overlap area forms a rhombus shape and can be calculated using dimensions found by the intersection points:\\n1. The height of each triangle = \\\\( 4 \\\\)\\n2. Width (base) = \\\\( 4 \\\\) from points determined above.\\n\\nThe area \\\\( A \\\\) of each triangle's intersection can be calculated as:\\n\\\\[\\nA = \\\\frac{1}{2} \\\\times base \\\\times height = \\\\frac{1}{2} \\\\times 4 \\\\times 4 = 8\\n\\\\]\\n\\n### Step 7: How do we express the final area in simplest radical form?\\n\\nThus, the area common to the two triangles is:\\n\\\\[\\n\\\\text{Area} = 8 \\\\text{ square inches}\\n\\\\]\\n\\nTherefore, the area of the region that is common to the two triangles is \\\\( \\\\boxed{8} \\\\) square inches.\"}\n",
      "{'role': 'user', 'content': 'Please provide a confidence rating for the accuracy of this solution, on a scale from 1 to 10. Only output the number.'}\n",
      "total tokens: 1487\n",
      "total tokens: 1421\n",
      "{'role': 'user', 'content': '\\nThere is a math_problem. I need you to solve it and give an answer.\\nHere is the problem:\\nBy joining alternate vertices of a regular hexagon with edges $4$ inches long, two equilateral triangles are formed, as shown. What is the area, in square inches, of the region that is common to the two triangles? Express your answer in simplest radical form. [asy]\\ndraw((0,3)--(0,8)--(4,11)--(8,8)--(8,3)--(4,0)--cycle,black+linewidth(1));\\ndraw((4,0)--(0,8)--(8,8)--cycle, black+dashed+linewidth(1));\\ndraw((0,3)--(4,11)--(8,3)--cycle, black+dotted+linewidth(1));\\nlabel(\"4\",(8,5.5),E);\\n[/asy]\\n\\nI have broken this math problem down into a series of smaller problems. Here is the list of sub-problems: {0: \\'What are the coordinates of the vertices of the regular hexagon?\\', 1: \\'Which vertices are used to form the two equilateral triangles?\\', 2: \\'What are the lengths of the sides of the equilateral triangles formed?\\', 3: \\'What are the coordinates of the vertices of the first equilateral triangle?\\', 4: \\'What are the coordinates of the vertices of the second equilateral triangle?\\', 5: \\'How do we find the intersection points of the two equilateral triangles?\\', 6: \\'What is the area of the region formed by the intersection of the two triangles?\\', 7: \\'How do we express the final area in simplest radical form?\\'}\\nPlease solve the problem and respond according to mathematical logic.\\n    \\n    '}\n",
      "{'role': 'assistant', 'content': 'To solve the problem of finding the area common to the two equilateral triangles formed by joining alternate vertices of a regular hexagon with edges 4 inches long, we can break it down step-by-step based on your outlined sub-problems.\\n\\n### Step 0: Coordinates of the Vertices of the Regular Hexagon\\nWe can place the regular hexagon centered at the origin with vertices as follows:\\n- \\\\((2, 2\\\\sqrt{3})\\\\)\\n- \\\\((4, 0)\\\\)\\n- \\\\((2, -2\\\\sqrt{3})\\\\)\\n- \\\\((-2, -2\\\\sqrt{3})\\\\)\\n- \\\\((-4, 0)\\\\)\\n- \\\\((-2, 2\\\\sqrt{3})\\\\)\\n\\nUsing the geometry of a regular hexagon, each vertex is at a distance of 4 inches from the center.\\n\\n### Step 1: Vertices Used to Form the Two Equilateral Triangles\\nFor the first equilateral triangle, we can choose:\\n- Vertex 1: \\\\((2, 2\\\\sqrt{3})\\\\)\\n- Vertex 3: \\\\((2, -2\\\\sqrt{3})\\\\)\\n- Vertex 5: \\\\((-4, 0)\\\\)\\n\\nFor the second equilateral triangle, we can choose:\\n- Vertex 2: \\\\((4, 0)\\\\)\\n- Vertex 4: \\\\((-2, -2\\\\sqrt{3})\\\\)\\n- Vertex 6: \\\\((-2, 2\\\\sqrt{3})\\\\)\\n\\n### Step 2: Length of the Sides of the Equilateral Triangles\\nSince this is a regular hexagon and we are using its vertices directly, the sides of the equilateral triangles will also be 4 inches.\\n\\n### Step 3: Coordinates of the Vertices of the First Equilateral Triangle\\nThe vertices of the first equilateral triangle are:\\n- \\\\(A = (2, 2\\\\sqrt{3})\\\\)\\n- \\\\(B = (2, -2\\\\sqrt{3})\\\\)\\n- \\\\(C = (-4, 0)\\\\)\\n\\n### Step 4: Coordinates of the Vertices of the Second Equilateral Triangle\\nThe vertices of the second equilateral triangle are:\\n- \\\\(D = (4, 0)\\\\)\\n- \\\\(E = (-2, -2\\\\sqrt{3})\\\\)\\n- \\\\(F = (-2, 2\\\\sqrt{3})\\\\)\\n\\n### Step 5: Finding the Intersection Points of the Two Equilateral Triangles\\nTo find the intersection of the two triangles, we can solve the equations of their sides to find any intersection points.\\n\\n1. Line \\\\(AB\\\\) from \\\\(A(2, 2\\\\sqrt{3})\\\\) to \\\\(B(2, -2\\\\sqrt{3})\\\\): This is a vertical line at \\\\(x=2\\\\).\\n2. Line \\\\(DE\\\\) from \\\\(D(4, 0)\\\\) to \\\\(E(-2, -2\\\\sqrt{3})\\\\): The slope is \\\\(\\\\frac{-2\\\\sqrt{3}}{-2-4}=\\\\frac{\\\\sqrt{3}}{3}\\\\), so the equation is \\\\(y=\\\\frac{\\\\sqrt{3}}{3}(x-4)\\\\).\\n3. Solving for intersection gives:\\n   \\\\[\\n   0 = \\\\frac{\\\\sqrt{3}}{3}(2 - 4) \\\\Rightarrow y = \\\\frac{\\\\sqrt{3}}{3} \\\\cdot (-2) = -\\\\frac{2\\\\sqrt{3}}{3}\\n   \\\\]\\n\\nSo, the coordinates of the intersection would be \\\\( (2, -\\\\frac{2\\\\sqrt{3}}{3})\\\\).\\n\\n4. Similarly, we can find other intersection points.\\n\\nFor this case and symmetry, we conclude that the common region is a shared smaller area created at the meeting point of both triangles which lies symmetrically around the center. \\n\\n### Step 6: Area of the Region Formed by the Intersection of the Two Triangles\\nThe area of one triangle can be calculated using the formula for the area of an equilateral triangle:\\n\\\\[\\n\\\\text{Area} = \\\\frac{\\\\sqrt{3}}{4} s^2 \\\\quad \\\\text{ where } s = 4.\\n\\\\]\\nCalculating gives:\\n\\\\[\\n\\\\text{Area of one triangle} = \\\\frac{\\\\sqrt{3}}{4} \\\\cdot (4^2) = 4\\\\sqrt{3} \\\\text{ square inches.}\\n\\\\]\\n\\n### Step 7: Expressing the Final Area in Simplest Radical Form\\nEach triangle contributes the same area at the overlap; the common intersection is smaller than each of them. Due to symmetry and overlap conditions that create sub-regions in each triangle, the area common specifically between them can be calculated as:\\n\\\\[\\n\\\\text{Common Area} = 2\\\\sqrt{3}.\\n\\\\]\\n\\nThus, the area of the region common to the two triangles formed is:\\n\\\\[\\n\\\\boxed{8 \\\\sqrt{3}}\\n\\\\] square inches.'}\n",
      "{'role': 'user', 'content': 'Please provide a confidence rating for the accuracy of this solution, on a scale from 1 to 10. Only output the number.'}\n",
      "total tokens: 1457\n",
      "total tokens: 1754\n",
      "{'role': 'user', 'content': '\\nThere is a math_problem. I need you to solve it and give an answer.\\nHere is the problem:\\nBy joining alternate vertices of a regular hexagon with edges $4$ inches long, two equilateral triangles are formed, as shown. What is the area, in square inches, of the region that is common to the two triangles? Express your answer in simplest radical form. [asy]\\ndraw((0,3)--(0,8)--(4,11)--(8,8)--(8,3)--(4,0)--cycle,black+linewidth(1));\\ndraw((4,0)--(0,8)--(8,8)--cycle, black+dashed+linewidth(1));\\ndraw((0,3)--(4,11)--(8,3)--cycle, black+dotted+linewidth(1));\\nlabel(\"4\",(8,5.5),E);\\n[/asy]\\n\\nI have broken this math problem down into a series of smaller problems. Here is the list of sub-problems: {0: \\'What are the coordinates of the vertices of the regular hexagon?\\', 1: \\'Which vertices are used to form the two equilateral triangles?\\', 2: \\'What are the lengths of the sides of the equilateral triangles formed?\\', 3: \\'What are the coordinates of the vertices of the first equilateral triangle?\\', 4: \\'What are the coordinates of the vertices of the second equilateral triangle?\\', 5: \\'How do we find the intersection points of the two equilateral triangles?\\', 6: \\'What is the area of the region formed by the intersection of the two triangles?\\', 7: \\'How do we express the final area in simplest radical form?\\'}\\nPlease solve the problem and respond according to mathematical logic.\\n    \\n    '}\n",
      "{'role': 'assistant', 'content': 'To solve the given problem, let’s go through the sub-problems step by step.\\n\\n### Step 0: Coordinates of the Vertices of the Regular Hexagon\\nA regular hexagon can be centered at the origin with its vertices positioned as follows. With each side of the hexagon measuring \\\\(4\\\\) inches, the vertices are located at:\\n- \\\\( A(4, 0) \\\\)\\n- \\\\( B(2, 2\\\\sqrt{3}) \\\\)\\n- \\\\( C(-2, 2\\\\sqrt{3}) \\\\)\\n- \\\\( D(-4, 0) \\\\)\\n- \\\\( E(-2, -2\\\\sqrt{3}) \\\\)\\n- \\\\( F(2, -2\\\\sqrt{3}) \\\\)\\n\\n### Step 1: Which Vertices are Used to Form the Two Equilateral Triangles?\\nBy joining alternate vertices, two equilateral triangles can be formed:\\n- Triangle 1 (using vertices \\\\( A \\\\), \\\\( C \\\\), \\\\( E \\\\)): vertices are \\\\( A(4, 0) \\\\), \\\\( C(-2, 2\\\\sqrt{3}) \\\\), \\\\( E(-2, -2\\\\sqrt{3}) \\\\).\\n- Triangle 2 (using vertices \\\\( B \\\\), \\\\( D \\\\), \\\\( F \\\\)): vertices are \\\\( B(2, 2\\\\sqrt{3}) \\\\), \\\\( D(-4, 0) \\\\), \\\\( F(2, -2\\\\sqrt{3}) \\\\).\\n\\n### Step 2: Lengths of the Sides of the Equilateral Triangles\\nEach side of the triangles is the distance between two chosen vertices. The side lengths are calculated as follows:\\n\\n- **For Triangle 1:**\\n  - Distance \\\\( AC \\\\):\\n    \\\\[\\n    d = \\\\sqrt{(4 - (-2))^2 + (0 - 2\\\\sqrt{3})^2} = \\\\sqrt{(6)^2 + (-2\\\\sqrt{3})^2} = \\\\sqrt{36 + 12} = \\\\sqrt{48} = 4\\\\sqrt{3}\\n    \\\\]\\n  - Distance \\\\( CE \\\\): \\n    \\\\[\\n    d = \\\\sqrt{((-2) - (-2))^2 + (2\\\\sqrt{3} - (-2\\\\sqrt{3}))^2} = \\\\sqrt{(0)^2 + (4\\\\sqrt{3})^2} = \\\\sqrt{48} = 4\\\\sqrt{3}\\n    \\\\]\\n  - Distance \\\\( EA \\\\):\\n    \\\\[\\n    d = \\\\sqrt{(4 - (-2))^2 + (0 - (-2\\\\sqrt{3}))^2} = \\\\sqrt{(6)^2 + (2\\\\sqrt{3})^2} = \\\\sqrt{36 + 12} = \\\\sqrt{48} = 4\\\\sqrt{3}\\n    \\\\]\\n\\nSo all sides of triangle \\\\( A, C, E \\\\) are \\\\( 4\\\\sqrt{3} \\\\).\\n\\n- **For Triangle 2:**\\n  - Distance \\\\( BD \\\\):\\n    \\\\[\\n    d = \\\\sqrt{(2 - (-4))^2 + (2\\\\sqrt{3} - 0)^2} = \\\\sqrt{(6)^2 + (2\\\\sqrt{3})^2} = \\\\sqrt{36 + 12} = \\\\sqrt{48} = 4\\\\sqrt{3}\\n    \\\\]\\n  - Distance \\\\( DF \\\\):\\n    \\\\[\\n    d = \\\\sqrt{((-4) - 2)^2 + (0 - (-2\\\\sqrt{3}))^2} = \\\\sqrt{(-6)^2 + (2\\\\sqrt{3})^2} = \\\\sqrt{36 + 12} = \\\\sqrt{48} = 4\\\\sqrt{3}\\n    \\\\]\\n  - Distance \\\\( FB \\\\):\\n    \\\\[\\n    d = \\\\sqrt{(2 - 2)^2 + (2\\\\sqrt{3} - (-2\\\\sqrt{3}))^2} = \\\\sqrt{0 + (4\\\\sqrt{3})^2} = \\\\sqrt{48} = 4\\\\sqrt{3}\\n    \\\\]\\n\\nThus, all sides of triangle \\\\( B, D, F \\\\) are also \\\\( 4\\\\sqrt{3} \\\\).\\n\\n### Steps 3 and 4: Coordinates of Vertices of the Triangles\\n- Triangle 1: \\\\( A(4, 0) \\\\), \\\\( C(-2, 2\\\\sqrt{3}) \\\\), \\\\( E(-2, -2\\\\sqrt{3}) \\\\)\\n- Triangle 2: \\\\( B(2, 2\\\\sqrt{3}) \\\\), \\\\( D(-4, 0) \\\\), \\\\( F(2, -2\\\\sqrt{3}) \\\\)\\n\\n### Step 5: Finding the Intersection Points\\nTo find the intersection points of the two triangles, we will locate the intersection of line segments that make up the sides of these triangles.\\n\\n1. Line segment \\\\( AC \\\\) intersects line segment \\\\( BD \\\\).\\n2. Line segment \\\\( CE \\\\) intersects line segment \\\\( DF \\\\).\\n3. Line segment \\\\( EA \\\\) intersects line segment \\\\( FB \\\\).\\n\\nBy solving the equations of these line segments:\\n\\n1. **Equation of Line \\\\( AC \\\\) and Line \\\\( BD \\\\)**.\\n   - Line \\\\( AC \\\\): points \\\\( A(4, 0), C(-2, 2\\\\sqrt{3}) \\\\).\\n   - Functioning similarly for \\\\( BD \\\\).\\n\\n2. You find the coordinates for the intersection points, solving (with a bit of algebra for the intersection of the lines) will yield the points of intersection.\\n\\n### Step 6: Area of the Region Formed by the Intersection\\nThe area can be calculated using the formula for the area of a triangle given its vertices, or by using the shoelace formula (if it is a polygon) based on the intersection coordinates.\\n\\nHowever, visualizing shows that the common region is typically a smaller equilateral triangle formed by the intersection. \\n\\n### Step 7: Express the Final Area in Simplest Radical Form\\nThe area \\\\( A \\\\) of the common region is equal to \\\\(\\\\frac{\\\\sqrt{3}}{4} s^2\\\\), where \\\\( s = 4 \\\\) (common edge length). \\n\\n\\\\[\\nA = \\\\frac{\\\\sqrt{3}}{4} (4)^2 = \\\\frac{\\\\sqrt{3}}{4} \\\\cdot 16 = 4\\\\sqrt{3}\\n\\\\]\\n\\nThe area of the region that is common to both triangles is \\\\(\\\\boxed{4\\\\sqrt{3}}\\\\) square inches.'}\n",
      "{'role': 'user', 'content': 'Please provide a confidence rating for the accuracy of this solution, on a scale from 1 to 10. Only output the number.'}\n",
      "total tokens: 1790\n",
      "best_solution:  To solve the given problem, let’s go through the sub-problems step by step.\n",
      "\n",
      "### Step 0: Coordinates of the Vertices of the Regular Hexagon\n",
      "A regular hexagon can be centered at the origin with its vertices positioned as follows. With each side of the hexagon measuring \\(4\\) inches, the vertices are located at:\n",
      "- \\( A(4, 0) \\)\n",
      "- \\( B(2, 2\\sqrt{3}) \\)\n",
      "- \\( C(-2, 2\\sqrt{3}) \\)\n",
      "- \\( D(-4, 0) \\)\n",
      "- \\( E(-2, -2\\sqrt{3}) \\)\n",
      "- \\( F(2, -2\\sqrt{3}) \\)\n",
      "\n",
      "### Step 1: Which Vertices are Used to Form the Two Equilateral Triangles?\n",
      "By joining alternate vertices, two equilateral triangles can be formed:\n",
      "- Triangle 1 (using vertices \\( A \\), \\( C \\), \\( E \\)): vertices are \\( A(4, 0) \\), \\( C(-2, 2\\sqrt{3}) \\), \\( E(-2, -2\\sqrt{3}) \\).\n",
      "- Triangle 2 (using vertices \\( B \\), \\( D \\), \\( F \\)): vertices are \\( B(2, 2\\sqrt{3}) \\), \\( D(-4, 0) \\), \\( F(2, -2\\sqrt{3}) \\).\n",
      "\n",
      "### Step 2: Lengths of the Sides of the Equilateral Triangles\n",
      "Each side of the triangles is the distance between two chosen vertices. The side lengths are calculated as follows:\n",
      "\n",
      "- **For Triangle 1:**\n",
      "  - Distance \\( AC \\):\n",
      "    \\[\n",
      "    d = \\sqrt{(4 - (-2))^2 + (0 - 2\\sqrt{3})^2} = \\sqrt{(6)^2 + (-2\\sqrt{3})^2} = \\sqrt{36 + 12} = \\sqrt{48} = 4\\sqrt{3}\n",
      "    \\]\n",
      "  - Distance \\( CE \\): \n",
      "    \\[\n",
      "    d = \\sqrt{((-2) - (-2))^2 + (2\\sqrt{3} - (-2\\sqrt{3}))^2} = \\sqrt{(0)^2 + (4\\sqrt{3})^2} = \\sqrt{48} = 4\\sqrt{3}\n",
      "    \\]\n",
      "  - Distance \\( EA \\):\n",
      "    \\[\n",
      "    d = \\sqrt{(4 - (-2))^2 + (0 - (-2\\sqrt{3}))^2} = \\sqrt{(6)^2 + (2\\sqrt{3})^2} = \\sqrt{36 + 12} = \\sqrt{48} = 4\\sqrt{3}\n",
      "    \\]\n",
      "\n",
      "So all sides of triangle \\( A, C, E \\) are \\( 4\\sqrt{3} \\).\n",
      "\n",
      "- **For Triangle 2:**\n",
      "  - Distance \\( BD \\):\n",
      "    \\[\n",
      "    d = \\sqrt{(2 - (-4))^2 + (2\\sqrt{3} - 0)^2} = \\sqrt{(6)^2 + (2\\sqrt{3})^2} = \\sqrt{36 + 12} = \\sqrt{48} = 4\\sqrt{3}\n",
      "    \\]\n",
      "  - Distance \\( DF \\):\n",
      "    \\[\n",
      "    d = \\sqrt{((-4) - 2)^2 + (0 - (-2\\sqrt{3}))^2} = \\sqrt{(-6)^2 + (2\\sqrt{3})^2} = \\sqrt{36 + 12} = \\sqrt{48} = 4\\sqrt{3}\n",
      "    \\]\n",
      "  - Distance \\( FB \\):\n",
      "    \\[\n",
      "    d = \\sqrt{(2 - 2)^2 + (2\\sqrt{3} - (-2\\sqrt{3}))^2} = \\sqrt{0 + (4\\sqrt{3})^2} = \\sqrt{48} = 4\\sqrt{3}\n",
      "    \\]\n",
      "\n",
      "Thus, all sides of triangle \\( B, D, F \\) are also \\( 4\\sqrt{3} \\).\n",
      "\n",
      "### Steps 3 and 4: Coordinates of Vertices of the Triangles\n",
      "- Triangle 1: \\( A(4, 0) \\), \\( C(-2, 2\\sqrt{3}) \\), \\( E(-2, -2\\sqrt{3}) \\)\n",
      "- Triangle 2: \\( B(2, 2\\sqrt{3}) \\), \\( D(-4, 0) \\), \\( F(2, -2\\sqrt{3}) \\)\n",
      "\n",
      "### Step 5: Finding the Intersection Points\n",
      "To find the intersection points of the two triangles, we will locate the intersection of line segments that make up the sides of these triangles.\n",
      "\n",
      "1. Line segment \\( AC \\) intersects line segment \\( BD \\).\n",
      "2. Line segment \\( CE \\) intersects line segment \\( DF \\).\n",
      "3. Line segment \\( EA \\) intersects line segment \\( FB \\).\n",
      "\n",
      "By solving the equations of these line segments:\n",
      "\n",
      "1. **Equation of Line \\( AC \\) and Line \\( BD \\)**.\n",
      "   - Line \\( AC \\): points \\( A(4, 0), C(-2, 2\\sqrt{3}) \\).\n",
      "   - Functioning similarly for \\( BD \\).\n",
      "\n",
      "2. You find the coordinates for the intersection points, solving (with a bit of algebra for the intersection of the lines) will yield the points of intersection.\n",
      "\n",
      "### Step 6: Area of the Region Formed by the Intersection\n",
      "The area can be calculated using the formula for the area of a triangle given its vertices, or by using the shoelace formula (if it is a polygon) based on the intersection coordinates.\n",
      "\n",
      "However, visualizing shows that the common region is typically a smaller equilateral triangle formed by the intersection. \n",
      "\n",
      "### Step 7: Express the Final Area in Simplest Radical Form\n",
      "The area \\( A \\) of the common region is equal to \\(\\frac{\\sqrt{3}}{4} s^2\\), where \\( s = 4 \\) (common edge length). \n",
      "\n",
      "\\[\n",
      "A = \\frac{\\sqrt{3}}{4} (4)^2 = \\frac{\\sqrt{3}}{4} \\cdot 16 = 4\\sqrt{3}\n",
      "\\]\n",
      "\n",
      "The area of the region that is common to both triangles is \\(\\boxed{4\\sqrt{3}}\\) square inches.\n",
      "******************************评估结果******************************\n",
      "total tokens: 1440\n",
      "\\(\\boxed{4\\sqrt{3}}\\) square inches.\n",
      "total tokens: 265\n",
      "error\n"
     ]
    }
   ],
   "source": [
    "# TOT 第二种求解方式\n",
    "\n",
    "N = 3  # 先进性N次任务分解，然后 N次做题，最后评估\n",
    "\n",
    "\n",
    "# 任务分解阶段\n",
    "decompositions = []\n",
    "for i in range(N):\n",
    "    plan, Q = decompose_sql(question, type)\n",
    "    result, result_dict = convert_steps_to_format(plan)\n",
    "    \n",
    "    eval_Q = Q + [{'role':'assistant', 'content':f\"\"\"The plan is : {result}\"\"\"}]\n",
    "    eval_Q = eval_Q + [{'role':'user', 'content':\"Please provide a confidence rating for the accuracy of this step to step plan, on a scale from 1 to 10. Only output the number.\"}]\n",
    "    printSeq(eval_Q)\n",
    "    score = askChatGPT(eval_Q, model=GPT_MODEL, temperature=1)\n",
    "    score = int(score)\n",
    "    \n",
    "    decompositions.append((score, result_dict))\n",
    "\n",
    "# 选择最佳任务分解方案\n",
    "task_decompositions = sorted(decompositions, key=lambda x: x[0], reverse=True)\n",
    "best_task_decomposition = task_decompositions[0][1] \n",
    "\n",
    "print(\"******************************解题阶段******************************\")\n",
    "# 解题阶段\n",
    "solutions = []\n",
    "for i in range(N):\n",
    "    question_input = f\"\"\"\n",
    "There is a math_problem. I need you to solve it and give an answer.\n",
    "Here is the problem:\\n{question}\n",
    "\n",
    "I have broken this math problem down into a series of smaller problems. Here is the list of sub-problems: {best_task_decomposition}\n",
    "Please solve the problem and respond according to mathematical logic.\n",
    "    \n",
    "    \"\"\"\n",
    "    query = {\"role\": \"user\", \"content\": question_input}\n",
    "    Q_jie = [query]\n",
    "    sol = askChatGPT(Q_jie, model=GPT_MODEL, temperature=1)\n",
    "    sol_eval_Q = Q_jie + [{'role':'assistant', 'content':sol}]\n",
    "    sol_eval_Q = sol_eval_Q + [{'role':'user', 'content':\"Please provide a confidence rating for the accuracy of this solution, on a scale from 1 to 10. Only output the number.\"}]\n",
    "    printSeq(sol_eval_Q)\n",
    "    sol_score = askChatGPT(sol_eval_Q, model=GPT_MODEL, temperature=1)\n",
    "    sol_score = int(sol_score)\n",
    "    \n",
    "    solutions.append((sol_score, sol))\n",
    "\n",
    "# 选择最佳解答\n",
    "sub_solutions = sorted(solutions, key=lambda x: x[0], reverse=True)\n",
    "best_solution = sub_solutions[0][1]  # 选择最好的子问题解\n",
    "\n",
    "print(\"best_solution: \", best_solution)\n",
    "\n",
    "print(\"******************************评估结果******************************\")\n",
    "\n",
    "last_query = [{\n",
    "    \"role\": \"user\",\n",
    "    \"content\": f\"\"\" \n",
    "    Given the problem and step-by-step plan and the solution as following: {best_solution}\n",
    "    \n",
    "    What is the final answer?\n",
    "    Please give the final answer without any additional explanation or clarification.\"\"\"\n",
    "}]\n",
    "\n",
    "finalResult = askChatGPT(last_query, model=GPT_MODEL, temperature=1)\n",
    "print(finalResult)\n",
    "\n",
    "\n",
    "# 让大语言模型来判断有没有回答正确\n",
    "judgeAnswer = {'role':'user', 'content':f\"\"\"Here is a math problem with a standard answer and a student's solution. Please help me determine if the student's solution is correct. If the numerical value are same, then it is correct.\n",
    "\n",
    "Problem: {question}\n",
    "\n",
    "Standard answer: {gold_answer}\n",
    "\n",
    "Answer: {finalResult}\n",
    "\n",
    "If the student's answer is correct, just output True; otherwise, just output False.\n",
    "No explanation is required.\n",
    "\"\"\"}\n",
    "\n",
    "Q_judge = [judgeAnswer]\n",
    "ifcorrect = askChatGPT(Q_judge, model=GPT_MODEL, temperature=1)  # 要么是True, 要么是False\n",
    "\n",
    "if 'True' in ifcorrect:\n",
    "    print('correct')\n",
    "    success = True  # 任务未受中断,完整地结束了,所以标记为成功\n",
    "elif 'False' in ifcorrect:\n",
    "    print('error')\n",
    "    success = True  # 任务未受中断,完整地结束了,所以标记为成功                       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\(\\boxed{4\\sqrt{3}}\\) square inches.\n"
     ]
    }
   ],
   "source": [
    "print(finalResult)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\\sqrt{3} \\text { square inches}\n"
     ]
    }
   ],
   "source": [
    "print(gold_answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'sub_solutions' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[15], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m printSeq(\u001b[43msub_solutions\u001b[49m)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'sub_solutions' is not defined"
     ]
    }
   ],
   "source": [
    "printSeq(sub_solutions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The final answer is (-1 + √5)/2, (1 + √5)/2 and (-1 - √5)/2, (1 - √5)/2.'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "finalResult"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'To find the $x$-coordinates of the intersections, substitute $x^2$ for $y$ in $x+y=1$ and solve for $x$, resulting in  \\\\begin{align*}\\nx+x^2&=1 \\\\\\\\\\n\\\\Rightarrow \\\\qquad x^2+x-1&=0 \\\\\\\\\\n\\\\Rightarrow \\\\qquad x&=\\\\frac{-1\\\\pm\\\\sqrt{1+4}}2=\\\\frac{-1\\\\pm\\\\sqrt5}2\\\\\\\\\\n\\\\end{align*}Using each of these coordinates to solve for $y$ gives us the intersections at $\\\\left(\\\\frac{-1+\\\\sqrt5}2,\\\\frac{3-\\\\sqrt5}2\\\\right)$ and $\\\\left(\\\\frac{-1-\\\\sqrt5}2,\\\\frac{3+\\\\sqrt5}2\\\\right)$.  Using the distance formula, we have \\\\begin{align*}\\n&\\\\sqrt{ \\\\left(\\\\frac{-1+\\\\sqrt5}{2}-\\\\frac{-1-\\\\sqrt5}{2}\\\\right)^2 + \\\\left(\\\\frac{3-\\\\sqrt5}2-\\\\frac{3+\\\\sqrt5}2\\\\right)^2 }\\\\\\\\\\n&\\\\qquad=\\\\sqrt{\\\\left(\\\\frac{2\\\\sqrt5}2\\\\right)^2 + \\\\left(-\\\\frac{2\\\\sqrt5}2\\\\right)^2}\\\\\\\\\\n&\\\\qquad=\\\\sqrt{ 2\\\\sqrt5^2 }\\\\\\\\\\n&\\\\qquad=\\\\boxed{\\\\sqrt{10}}.\\n\\\\end{align*}'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gold_answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(9,\n",
       "  ['To find the intersection points of the two equations $y=x^2$ and $x+y=1$, we can substitute $y=x^2$ into the second equation to get:\\n$x + x^2 = 1$\\nwhich simplifies to:\\n$x^2 + x - 1 = 0$\\n\\nSolving this quadratic equation will give us the x-coordinates of the intersection points.',\n",
       "   'To find the intersection points, we need to solve the quadratic equation $x^2 + x - 1 = 0$. By using the quadratic formula, we find that the solutions are $x = \\\\frac{-1 \\\\pm \\\\sqrt{5}}{2}$.\\n\\nNow, we need to calculate the distance between the two intersection points. Let the intersection points be $A\\\\left(\\\\frac{-1 + \\\\sqrt{5}}{2}, \\\\left(\\\\frac{-1 + \\\\sqrt{5}}{2}\\\\right)^2\\\\right)$ and $B\\\\left(\\\\frac{-1 - \\\\sqrt{5}}{2}, \\\\left(\\\\frac{-1 - \\\\sqrt{5}}{2}\\\\right)^2\\\\right)$.\\n\\nUsing the distance formula, the distance between points $A$ and $B$ is:\\n\\n$$d = \\\\sqrt{\\\\left(\\\\frac{-1 + \\\\sqrt{5}}{2} - \\\\frac{-1 - \\\\sqrt{5}}{2}\\\\right)^2 + \\\\left(\\\\left(\\\\frac{-1 + \\\\sqrt{5}}{2}\\\\right)^2 - \\\\left(\\\\frac{-1 - \\\\sqrt{5}}{2}\\\\right)^2\\\\right)}$$\\n\\n$$d = \\\\sqrt{\\\\left(\\\\sqrt{5}\\\\right)^2 + \\\\left(\\\\frac{3 - 2\\\\sqrt{5}}{4} + \\\\frac{3 + 2\\\\sqrt{5}}{']),\n",
       " (9,\n",
       "  ['To find the intersection points of the two equations $y=x^2$ and $x+y=1$, we can substitute $y=x^2$ into the second equation to get:\\n$x + x^2 = 1$\\nwhich simplifies to:\\n$x^2 + x - 1 = 0$\\n\\nSolving this quadratic equation will give us the x-coordinates of the intersection points.',\n",
       "   'Given the quadratic equation $x^2 + x - 1 = 0$, we can solve for the x-coordinates of the intersection points as follows:\\n\\n$x = \\\\frac{-b \\\\pm \\\\sqrt{b^2 - 4ac}}{2a}$\\n\\nIn this case, $a=1$, $b=1$, and $c=-1$. Plugging these values into the formula gives:\\n\\n$x = \\\\frac{-1 \\\\pm \\\\sqrt{1^2 - 4(1)(-1)}}{2(1)}$\\n$x = \\\\frac{-1 \\\\pm \\\\sqrt{1 + 4}}{2}$\\n$x = \\\\frac{-1 \\\\pm \\\\sqrt{5}}{2}$\\n\\nTherefore, the x-coordinates of the intersection points are $\\\\frac{-1 + \\\\sqrt{5}}{2}$ and $\\\\frac{-1 - \\\\sqrt{5}}{2}$.\\n\\nThe distance between two points $(x_1, y_1)$ and $(x_2, y_2)$ in a plane can be calculated using the distance formula:\\n\\nDistance = $\\\\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$\\n\\nApplying this formula to the intersection points on the curve $y=x^2$, we find:\\n\\nDistance $= \\\\sqrt{\\\\left(\\\\frac{-1 - \\\\sqrt{5}}{2} - \\\\frac{-1 + \\\\sqrt'])]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['To find the intersection points, we will first substitute $y=x^2$ into $x+y=1$:\\n\\n$ x + x^2 = 1$\\n\\nRewriting it as a quadratic equation:\\n\\n$ x^2 + x - 1 = 0$\\n\\nSolving this quadratic equation using the quadratic formula $x = \\\\frac{-b \\\\pm \\\\sqrt{b^2 - 4ac}}{2a}$ with $a=1$, $b=1$, and $c=-1$, we get:\\n\\n$x = \\\\frac{-1 \\\\pm \\\\sqrt{1^2 - 4(1)(-1)}}{2(1)} $\\n\\n$x = \\\\frac{-1 \\\\pm \\\\sqrt{1 + 4}}{2} $\\n\\n$x = \\\\frac{-1 \\\\pm \\\\sqrt{5}}{2} $\\n\\nSo, the intersection points are $(-\\\\frac{1 + \\\\sqrt{5}}{2}, \\\\frac{1 + \\\\sqrt{5}}{2})$ and $(-\\\\frac{1 - \\\\sqrt{5}}{2}, \\\\frac{1 - \\\\sqrt{5}}{2})$.']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution[0][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "已经问完了所有的subtask,最后问一次得到最终的答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total tokens: 1670\n",
      "total tokens: 278\n",
      "error\n"
     ]
    }
   ],
   "source": [
    "# 从M个路径里挑一个最好的来问,也可以问完之后再评估一下选最好的答案\n",
    "\n",
    "user_q = f\"\"\"There is a math_problem:\\n{question}\n",
    "\n",
    "I have broken this math problem down into a series of smaller problems and each sub-problem is solved.\n",
    "The sub-problems and their corresponding answers are as follows. (Format: Sub-problem-Id: xxx; Sub-problem: xxx; Answer: xxx.)\"\"\"\n",
    "\n",
    "for index, value in enumerate(solution[0][1]):  # 这里仅仅使用了最终得分最高的1条路径来总结得到final answer\n",
    "    user_q += f\"\"\"\\nSub-problem-Id: {index}; Sub-problem: {steps[index]}; Answer: {value}.\"\"\"\n",
    "\n",
    "Q.append({'role':'user', 'content':f\"\"\"Now that all the sub-problems have been solved, so what is the final answer?\n",
    "Please give the final answer without any additional explanation or clarification.\"\"\"})\n",
    "finalResult = askChatGPT(Q, model=GPT_MODEL, temperature=1)\n",
    "\n",
    "# 让大语言模型来判断有没有回答正确\n",
    "judgeAnswer = {'role':'user', 'content':f\"\"\"Here is a math problem with a standard answer and a student's solution. Please help me determine if the student's solution is correct. If the numerical value are same, then it is correct.\n",
    "               \n",
    "Problem: {question}\n",
    "\n",
    "Standard answer: {gold_answer}\n",
    "\n",
    "Answer: {finalResult}\n",
    "\n",
    "If the student's answer is correct, just output True; otherwise, just output False.\n",
    "No explanation is required.\n",
    "\"\"\"}\n",
    "\n",
    "Q_judge = [judgeAnswer]\n",
    "ifcorrect = askChatGPT(Q_judge, model=GPT_MODEL, temperature=1)  # 要么是True, 要么是False\n",
    "\n",
    "if 'True' in ifcorrect:\n",
    "    print('correct')\n",
    "    success = True  # 任务未受中断,完整地结束了,所以标记为成功\n",
    "elif 'False' in ifcorrect:\n",
    "    print('error')\n",
    "    success = True  # 任务未受中断,完整地结束了,所以标记为成功                       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The area of the region that is common to the two triangles is \\( 3\\sqrt{3} \\) square inches.\n",
      "8\\sqrt{3} \\text { square inches}\n"
     ]
    }
   ],
   "source": [
    "print(finalResult)\n",
    "print(gold_answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
